package de.cbf.cam.dsa.facet.armor.editor;

import java.text.NumberFormat;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.ColumnLabelProvider;

import de.cbf.cam.dsa.facet.armor.helper.ConstantsArmorZoneFactors;
import de.cbf.cam.dsa.facet.armor.helper.ZoneFactors;
import de.cbf.cam.dsa.facet.armor.helper.editing_support.EditingSupportZoneFactors;
import de.cbf.cam.dsa.facet.armor.helper.editing_support.ZoneFactorsLabelProvider;
import de.cbf.util.ui.controller.ControllerTableEdit;
import de.cbf.util.ui.helper.ColumnDescription;

class ControllerEditZoneFactors extends ControllerTableEdit<ZoneFactors>{
	private static final Log logger = LogFactory.getLog(ControllerEditZoneFactors.class);
	
	public static final String TEXT_STANDARD_TRUE	= "standard";
	public static final String TEXT_STANDARD_FALSE	= "different";

	public static final String TEXT_COLUMN_HEADER_STANDARD		= "Test:isStandard";
	public static final String TEXT_COLUMN_HEADER_RS_HEAD		= "Rs:"+ConstantsArmorZoneFactors.ZONE_HEAD;
	public static final String TEXT_COLUMN_HEADER_RS_BACK		= "Rs:"+ConstantsArmorZoneFactors.ZONE_BACK;
	public static final String TEXT_COLUMN_HEADER_RS_CHEST		= "Rs:"+ConstantsArmorZoneFactors.ZONE_CHEST;
	public static final String TEXT_COLUMN_HEADER_RS_STOMACH	= "Rs:"+ConstantsArmorZoneFactors.ZONE_STOMACH;
	public static final String TEXT_COLUMN_HEADER_RS_ARM_LEFT	= "Rs:"+ConstantsArmorZoneFactors.ZONE_ARM_LEFT;
	public static final String TEXT_COLUMN_HEADER_RS_ARM_RIGHT	= "Rs:"+ConstantsArmorZoneFactors.ZONE_ARM_RIGHT;
	public static final String TEXT_COLUMN_HEADER_RS_LEG_LEFT	= "Rs:"+ConstantsArmorZoneFactors.ZONE_LEG_LEFT;
	public static final String TEXT_COLUMN_HEADER_RS_LEG_RIGHT	= "Rs:"+ConstantsArmorZoneFactors.ZONE_LEG_RIGHT;

	public static final Integer WIDTH_COLUMN_FACTOR = 50;

	protected NumberFormat format;
	
	protected ControllerEditZoneFactors() {
		super(ZoneFactors.class);
		format = NumberFormat.getNumberInstance();
	}

	
	
	/* (non-Javadoc)
	 * @see de.cbf.util.ui.controller.ControllerTableEdit#getColumnDescriptions()
	 */
	@Override
	protected List<ColumnDescription> getColumnDescriptions() {
		List<ColumnDescription> resultList = super.getColumnDescriptions();
		ColumnDescription cd_Standard = createCD_Standard();
		resultList.add(cd_Standard);
		
		ColumnDescription cd_RS; 
		cd_RS = createCD_RS_Head();		
		resultList.add(cd_RS);
		cd_RS = createCD_RS_Back();		
		resultList.add(cd_RS);
		cd_RS = createCD_RS_Chest();		
		resultList.add(cd_RS);
		cd_RS = createCD_RS_Stomach();		
		resultList.add(cd_RS);
		cd_RS = createCD_RS_ArmLeft();		
		resultList.add(cd_RS);
		cd_RS = createCD_RS_ArmRight();
		resultList.add(cd_RS);
		cd_RS = createCD_RS_LegLeft();		
		resultList.add(cd_RS);
		cd_RS = createCD_RS_LegRight();		
		resultList.add(cd_RS);
		
		return resultList;
	}

	protected ColumnDescription createCD_Standard(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_STANDARD);
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				if (element instanceof ZoneFactors) {
					ZoneFactors zoneFactors = (ZoneFactors) element;
					if(zoneFactors.getStandard()){
						return TEXT_STANDARD_TRUE;
					}else{
						return TEXT_STANDARD_FALSE;
					}					 
				} else {
					String err_msg = "entity of wrong type in getText : \n"
							+ "Expected: "
							+ ZoneFactors.class.getSimpleName()
							+ "\n"
							+ "Got: "
							+ (element == null ? "null" : element
									.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return super.getText(element);
			}			
		});
		columnDescription.setEditable(false);
		columnDescription.setWidth(20);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_Head(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_HEAD);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_HEAD);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_HEAD);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_Back(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_BACK);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_BACK);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_BACK);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_Chest(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_CHEST);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_CHEST);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_CHEST);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_Stomach(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_STOMACH);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_STOMACH);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_STOMACH);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_ArmLeft(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_ARM_LEFT);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_ARM_LEFT);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_ARM_LEFT);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_ArmRight(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_ARM_RIGHT);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_ARM_RIGHT);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_ARM_RIGHT);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_LegLeft(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_LEG_LEFT);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_LEG_LEFT);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_LEG_LEFT);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}
	
	protected ColumnDescription createCD_RS_LegRight(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setTitle(TEXT_COLUMN_HEADER_RS_LEG_RIGHT);
		ZoneFactorsLabelProvider labelProvider = new ZoneFactorsLabelProvider();
		labelProvider.setFormat(format);
		labelProvider.setZoneName(ConstantsArmorZoneFactors.ZONE_LEG_RIGHT);		
		columnDescription.setLabelProvider(labelProvider);
		columnDescription.setEditable(true);
		columnDescription.setEditSupportClass(EditingSupportZoneFactors.class);
		columnDescription.setEditingSupportCustomizeObject(ConstantsArmorZoneFactors.ZONE_LEG_RIGHT);
		columnDescription.setWidth(WIDTH_COLUMN_FACTOR);
		return columnDescription;
	}

	/* (non-Javadoc)
	 * @see de.cbf.util.ui.controller.ControllerTableEdit#handleRemove()
	 */
	@Override
	public void handleRemove() {
		//should only be one line
		ZoneFactors item = modelTable.get(0);
		item.resetAllZoneFactors();
		updateTable();
		updateSelectionButtonState();
	}
}